package com.sgb.kjwl.view.ui.fragment.packers;

import android.content.Context;
import androidx.databinding.DataBindingUtil;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sgb.kjwl.AppFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragmentPersonIntroductionBinding;
import com.sgb.kjwl.di.packers.DaggerPackersComponent;
import com.sgb.kjwl.di.packers.PackersModule;
import com.sgb.kjwl.model.entity.PackersPersonDetailEntity;
import com.sgb.kjwl.model.entity.PackersSkillsEntity;
import com.sgb.kjwl.view.ui.activity.packers.PackersPersonDetailActivity;
import com.sgb.kjwl.view.ui.adapter.packers.PackersPersonPapersAdapter;
import com.sgb.kjwl.viewmodel.packers.PersonIntroductionFragModel;
import com.swgk.core.base.di.AppComponent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.jetbrains.annotations.NotNull;

import javax.inject.Inject;

public class PersonIntroductionFragment extends AppFragment {

    private FragmentPersonIntroductionBinding binding;
    private PackersPersonDetailActivity activity;

    @Inject
    PersonIntroductionFragModel viewModel;

    @Override
    public void onAttach(@NotNull Context context) {
        super.onAttach(context);
        if (context instanceof PackersPersonDetailActivity) {
            activity = (PackersPersonDetailActivity) context;
        }
    }

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        DaggerPackersComponent.builder().appComponent(appComponent).packersModule(new PackersModule(this.activity)).build().inject(this.activity);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_person_introduction, container, false);
        EventBus.getDefault().register(this);
        return binding.getRoot();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN,sticky = true)
    public void onMessageEvent(PackersPersonDetailEntity detailEntity) {
        initView(detailEntity);
        }

    @Subscribe(threadMode = ThreadMode.MAIN,sticky = true)
    public void onMessageEvent(PackersSkillsEntity packersSkillsEntity) {
        initSkillsView(packersSkillsEntity);
    }

    private void initSkillsView(PackersSkillsEntity packersSkillsEntity) {

        //往容器内添加TextView数据
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(10, 5, 10, 5);
        if (binding.flSkills != null) {
            binding.flSkills.removeAllViews();
        }
        for (int i = 0; i < packersSkillsEntity.getTypes().size(); i++) {
            TextView tv = new TextView(this.getActivity());
            tv.setPadding(28, 10, 28, 10);
            tv.setText(packersSkillsEntity.getTypes().get(i).getSkill());
            tv.setMaxEms(10);
            tv.setSingleLine();
            tv.setTextColor(getResources().getColor(R.color.color_0286DF));
            tv.setBackgroundResource(R.drawable.corner_blue_shape);
            tv.setLayoutParams(layoutParams);
            binding.flSkills.addView(tv, layoutParams);
        }

        if (packersSkillsEntity.getImgs()==null || packersSkillsEntity.getImgs().size() == 0){
            binding.tvPlaceholder.setVisibility(View.VISIBLE);
        }else {
            binding.tvPlaceholder.setVisibility(View.GONE);
        }
        //证件展示
        LinearLayoutManager layoutManager = new LinearLayoutManager(this.getActivity() );
        //设置布局管理器
        binding.rvPapers.setLayoutManager(layoutManager);
        //设置为垂直布局，这也是默认的
        layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        //设置Adapter
        PackersPersonPapersAdapter papersAdapter = new PackersPersonPapersAdapter(this.getActivity(), packersSkillsEntity.getImgs());
        binding.rvPapers.setAdapter(papersAdapter);
    }


    private void initView(PackersPersonDetailEntity detailEntity) {
        binding.tvName.setText(detailEntity.getName());
        binding.tvAge.setText(detailEntity.getAgeNum()+"");
        binding.tvArea.setText(detailEntity.getArea());
        binding.tvWorkYears.setText(detailEntity.getWorkYears()+"");
        binding.tvServiceArea.setText(detailEntity.getCategoryName());
        if (detailEntity.getIndividualResume() == null || detailEntity.getIndividualResume()==""){
            binding.tvIntroduction.setText("暂无介绍");
        }else {
            binding.tvIntroduction.setText(detailEntity.getIndividualResume());
        }
        if (detailEntity.getPerformance() == null || detailEntity.getPerformance() == ""){
            binding.tvAchievement.setText("暂无介绍");
        }else {
            binding.tvAchievement.setText(detailEntity.getPerformance());
        }

        //自我介绍
        openAndCloseText(binding.tvIntroduction, binding.tvOpenLine);

        //业绩介绍
        openAndCloseText(binding.tvAchievement, binding.tvOpenAchievement);

    }

    //展开收起文本
    public void openAndCloseText(TextView text, TextView button){
        text.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
                int lineCount = text.getLineCount();
                if (lineCount > 3){
                    text.setMaxLines(3);
                    button.setVisibility(View.VISIBLE);
                }else {
                    button.setVisibility(View.GONE);
                }
                text.getViewTreeObserver().removeOnPreDrawListener(this);
                return false;
            }
        });

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (button.getText().equals("展开")){
                    button.setText("收起");
                    text.setMaxLines(20);
                    //判断是否少于5行
                    text.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
                        @Override
                        public boolean onPreDraw() {
                            int lineCount = text.getLineCount();
                            if (lineCount <= 5){
                                button.setVisibility(View.GONE);
                            }else {
                                button.setVisibility(View.VISIBLE);
                            }
                            text.getViewTreeObserver().removeOnPreDrawListener(this);
                            return false;
                        }
                    });

                }else {
                    button.setText("展开");
                    text.setMaxLines(3);
                }
            }
        });
    }
}
